<?php
/**
 * 検索条件登録・削除API　モデルクラス<br />
 *
 * @package apamanshop
 * @author M.Matsutani
 * @copyright コピーライト(要確認)
 */
class Mypage_Model_Paraminsert extends Mypage_Model_Base
{
	/**
	 * マイページ検索条件の登録
	 *
	 * @param $params
	 * @return string $resCd エラーコード
	 */
	public function insertKensakuJouken($params)
	{

		$intUCD = $params['UserCd'];
		$strAreaCd = str_replace(" ","",str_replace(",","",$params['AreaCd']));
		$strPrefCd = str_replace(" ","",$params['PrefCd']);
		$strSkchsn = str_replace(" ","",$params['CityCd']);
		$strMachi = str_replace(" ","",$params['SectCd']);
		$strEnsen = str_replace(" ","",$params['RailCd']);
		$strEki = str_replace(" ","",$params['StCd']);
		$intTinryoMin = trim($params['tinryo1']);
		$intTinryoMax = trim($params['tinryo2']);
		$intEkitoho = $params['ekitoho'];
		$intSenyuMin = $params['senyu1'];
		$intSenyuMax = $params['senyu2'];
		$intBuilt = $params['built'];
		$strSrhKbn = $params['SrhKbn'];
		// 検索区分制御
		if ($strSrhKbn != Apm_Config_Search::SRHKBN_JYUSYO && $strSrhKbn != Apm_Config_Search::SRHKBN_ENSENEKI && $strSrhKbn != Apm_Config_Search::SRHKBN_SUMITAIEKI) {
			if (!empty($strSkchsn)) {
				$strSrhKbn = Apm_Config_Search::SRHKBN_JYUSYO;
			}
			else if (!empty($strEnsen)) {
				$strSrhKbn = Apm_Config_Search::SRHKBN_ENSENEKI;
			}
		}
		// 検索区分が無いとエラー
		if (empty($strSrhKbn)) return "9907";
		
		//初期費用
		$intsyokihiyo = $params['syokihiyo'];

		//管理費・共益費
		if ($params['tinryo_jouken2'] == "on")
		{
			$IntKanriKyoueki = 1;
		}else{
			$IntKanriKyoueki = 0;
		}
		//敷金・礼金0
		if ($params['shikireizero'] == "on")
		{
			$IntShikiRei = 1;
		}else{
			$IntShikiRei = 0;
		}
		//フリーレント
		if ($params['freerent'] == "on")
		{
			$intFreeRent = 1;
		}else{
			$intFreeRent = 0;
		}
		//部屋割
		if ($params['heyawari'] == "on")
		{
			$intHeyaWari = 1;
		}else{
			$intHeyaWari = 0;
		}
		//建物種別
		for ($i = 1; $i <= 7; $i++)
		{
			if ($params['syubetu' . $i] == "on")
			{
				if ($strTatemonoType <> "")
				{
					$strTatemonoType .= ",";
				}
				$strTatemonoType .= $i;
			}
		}
		//間取
		for ($i = 10; $i <= 51; $i++)
		{
			if ($params['madori' . $i] == "on")
			{
				if ($strMadori <> "")
				{
					$strMadori .= "," ;
				}
				$strMadori .= $i;
			}
		}
		//受渡用パラメータタグ設定処理
		$rosenzu = explode(",",$params['rosenzucd_sel']);
		if (!empty($rosenzu)) {
			if (empty($rosenzu[0])) { $rosenzu = array(); }
		}
		for ($i = 0; $i < count($rosenzu); $i++)
		{
			if ($strEki <> "")
			{
				$strEki .= ",";
			}
			$strEki .= trim($rosenzu[$i]);
		}

		//情報の新しさ
		$intJyohou = $params['jyohou'];
		if ($intJyohou == "")
		{
			$intJyohou = 0;
		}
		//通勤時間
		$strEndRangeTime = str_replace(" ","",$params['EndRangeTime']);
		$strTgtStCd = str_replace(" ","",$params['TgtStCd']);
		if ($strEndRangeTime == "" || $strTgtStCd == "")
		{
			$strCommuting = "";
		}else{
			$strCommuting = $strTgtStCd . "," . $strEndRangeTime;
		}
		//キャンペーン対象検索
		if ($params['campaign'] == "on" || $params['campaign2'] == "on"
		   || $params['campaign3'] == "on" || $params['campaign4'] == "on"
		   || $params['campaign5'] == "on" || $params['campaign6'] == "on"
		   || $params['campaign7'] == "on" || $params['campaign8'] == "on"
		   || $params['campaign9'] == "on" || $params['campaign10'] == "on")
		   {
		   	$intCampaign = 1;
		   }else{
		   	$intCampaign = 0;
		   }
		 //こだわり条件
		 $strKodawariJoken = $this->KodawariSet($params);

		 //駐車場代込み
		 if ($params['tyusyajyo_komi'] == "on")
		 {
		 	$intTyusyajoKomi = 1;
		 }else{
		 	$intTyusyajoKomi = 0;
		 }
		 //めやす賃料
		 if ($params['meyasu_tinryo'] == "meyasu")
		 {
		 	$intMeyasuTinryo = 1;
		 }else{
		 	$intMeyasuTinryo = 0;
		 }
		 //敷金なし
		 if ($params['shikikin_nashi'] == "on")
		 {
		 	$intShikikinNashi = 1;
		 }else{
		 	$intShikikinNashi = 0;
		 }
		 //礼金なし
		 if ($params['reikin_nashi'] == "on")
		 {
		 	$intReikinNashi = 1;
		 }else{
		 	$intReikinNashi = 0;
		 }
		 //バス利用可
		 if ($params['bas_riyou'] == "on")
		 {
		 	$intBasRiyou = 1;
		 }else{
		 	$intBasRiyou = 0;
		 }


		 //DBパラメータ用配列生成
    	 $db_params = array();

		 $sql = "";
	     $sql .= "SELECT COUNT(*) CNT";
	     $sql .= " ,MIN(T98_TOUROKUBI) MINDATE";
	     $sql .= " FROM ";
	     $sql .= " T98SEARCH_PARAM ";
	     $sql .= " WHERE ";
	     $sql .= " T98_USER_CD = " . $intUCD;

         //=====データ取得=====
		 $userDtl = $this->dbGetList($sql, $db_params);


		 $db = $this->getDbConnection();

		 if ($userDtl[0]['CNT'] <> 0)
		 {
		    //CNT_MY_JOKEN_MAX件を超える場合は登録日が一番古いものを削除
		 	if ($userDtl[0]['CNT'] > (Apm_Config_Mypage::CNT_MY_JOKEN_MAX - 1))
		    {
			    try
				{
					//トランザクション開始
				    $this->startTransaction($db);

				    $sql = "";
				  	$sql .= "DELETE FROM T98SEARCH_PARAM";
		          	$sql .= " WHERE T98_USER_CD = " . $intUCD;
		          	$sql .= " AND T98_TOUROKUBI = TO_DATE('" . $userDtl[0]['MINDATE'] . "','YYYY/MM/DD HH24:MI:SS')";

					//=====データ削除=====
					$this->dbExecQuery($sql, $db_params, $db,"T98SEARCH_PARAM");

					$this->commitTransaction($db);
				}
				catch (Exception $e)
				{
					$this->rollbackTransaction($db);
					$db->closeConnection();
					return "9907";
				}
		    }

		 }


		 try
		 {
			 //トランザクション開始
			 $this->startTransaction($db);

			 $sql = "";
			 $sql .= " INSERT INTO T98SEARCH_PARAM ";
			 $sql .= "(";
			 $sql .= " T98_USER_CD ";
			 $sql .= " ,T98_AREA_CD ";
			 $sql .= " ,T98_TODOUFUKEN_CD ";
			 $sql .= " ,T98_SHIKUCHOUSON_CD ";
			 $sql .= " ,T98_CHOUMEI_CD ";
			 $sql .= " ,T98_ENSEN_CD ";
			 $sql .= " ,T98_EKI_CD ";
			 $sql .= " ,T98_CHINRYO_MIN ";
			 $sql .= " ,T98_CHINRYO_MAX ";
			 $sql .= " ,T98_CHINRYO_JKN1 ";
			 $sql .= " ,T98_CHINRYO_JKN2 ";
			 $sql .= " ,T98_TATEMONO_CD ";
			 $sql .= " ,T98_EKI_TOHO ";
			 $sql .= " ,T98_SENYUU_MENSEKI_MIN";
			 $sql .= " ,T98_SENYUU_MENSEKI_MAX ";
			 $sql .= " ,T98_MADORI ";
			 $sql .= " ,T98_CHIKUNENN_KBN ";
			 $sql .= " ,T98_KOUSHIN_KBN ";
			 $sql .= " ,T98_SEARCH_TYPE ";
			 $sql .= " ,T98_COMMUTING ";
			 $sql .= " ,T98_KODAWARI_JYOKEN ";
			 $sql .= " ,T98_TOUROKUBI ";
			 $sql .= " ,T98_MYPAGE_KBN ";
			 $sql .= " ,T98_CAMPAIGN_FLAG ";
			 $sql .= " ,T98_CHINRYO_JKN3 ";
			 $sql .= " ,T98_CHINRYO_JKN4 ";
			 $sql .= " ,T98_PARKING_KOMI_FLAG ";
			 $sql .= " ,T98_MEYASU_CHINRYO_FLAG ";
			 $sql .= " ,T98_SHIKIKIN_ZERO_FLAG ";
			 $sql .= " ,T98_REIKIN_ZERO_FLAG ";
			 $sql .= " ,T98_BUS_RIYOU_FLAG ";
			 $sql .= " ,T98_SHOKI_HIYO_KBN ";
			 $sql .= " )";
			 $sql .= " VALUES ";
			 $sql .= " ( ";
			 $sql .= $intUCD . " ";
			 $sql .= " ,'" . $strAreaCd . "'";
			 $sql .= " ,'" . $strPrefCd . "'";
			 $sql .= " ,'" . $strSkchsn . "'";
			 $sql .= " ,'" . $strMachi . "'";
			 $sql .= " ,'" . $strEnsen . "'";
			 $sql .= " ,'" . $strEki . "'";
			 $sql .= " ,'" . $intTinryoMin . "'";
			 $sql .= " ,'" . $intTinryoMax . "'";
			 $sql .= " ,'" . $IntKanriKyoueki . "'";
			 $sql .= " ,'" . $IntShikiRei . "'";
			 $sql .= " ,'" . $strTatemonoType . "'";
			 $sql .= " ,'" . $intEkitoho . "'";
			 $sql .= " ,'" . $intSenyuMin . "'";
			 $sql .= " ,'" . $intSenyuMax . "'";
			 $sql .= " ,'" . $strMadori . "'";
			 $sql .= " ,'" . $intBuilt . "'";
			 $sql .= " ,'" . $intJyohou . "'";
			 $sql .= " ,'" . $strSrhKbn . "'";
			 $sql .= " ,'" . $strCommuting . "'";
			 $sql .= " ,'" . $strKodawariJoken . "'";
			 $sql .= " ,SYSDATE";
			 $sql .= " ,1";
			 $sql .= " ,'" . $intCampaign . "'";
			 $sql .= " ,'" . $intFreeRent . "'";
			 $sql .= " ,'" . $intHeyaWari . "'";
			 $sql .= " ,'" . $intTyusyajoKomi . "'";
			 $sql .= " ,'" . $intMeyasuTinryo . "'";
			 $sql .= " ,'" . $intShikikinNashi . "'";
			 $sql .= " ,'" . $intReikinNashi . "'";
			 $sql .= " ,'" . $intBasRiyou . "'";
			 $sql .= " ,'" . $intsyokihiyo . "'";
			 $sql .= " )";

			//=====データ登録=====
			$this->dbExecQuery($sql, $db_params, $db,"T98SEARCH_PARAM");

			$this->commitTransaction($db);
			$resCd = "0004";
         }
		 catch (Exception $e)
		 {
		 	$this->rollbackTransaction($db);
		 	$resCd = "9907";
		 }
		 $db->closeConnection();


	return $resCd;
	}

     /**
     * マイページ検索条件削除
     *
     * @param $params
     * @return string $resCd エラーコード
     */
    public function deleteKensakuJouken($params)
    {
    	//DBパラメータ用配列生成
    	$db_params = array();

    	try
		{
			$db = $this->getDbConnection();
			//トランザクション開始
		    $this->startTransaction($db);

		    $sql = "";
		  	$sql .= "DELETE FROM T98SEARCH_PARAM ";
          	$sql .= "WHERE T98_USER_CD = '" . $params['UID'] . "' ";
          	$sql .= "AND TO_CHAR(T98_TOUROKUBI, 'YYYY/MM/DD HH24:MI:SS')  = '" . $params['SID'] . "' ";

			//=====データ削除=====
			$this->dbExecQuery($sql, $db_params, $db,"T98SEARCH_PARAM");

			$this->commitTransaction($db);
			$resCd = "0005";
		}
		catch (Exception $e)
		{
			$this->rollbackTransaction($db);
			$resCd = "9908";
		}
		$db->closeConnection();

		return $resCd;
    }

        /**
     * マイページ検索条件更新
     *
     * @param $params
     * @return string $resCd エラーコード
     */
    public function updateKensakuJouken($params)
    {
    	//DBパラメータ用配列生成
    	$db_params = array();

    	try
		{
			$db = $this->getDbConnection();
			//トランザクション開始
		    $this->startTransaction($db);

		    $sql = "";
		  	$sql .= "DELETE FROM T98SEARCH_PARAM ";
          	$sql .= "WHERE T98_USER_CD = '" . $params['UserCd'] . "' ";
          	$sql .= "AND TO_CHAR(T98_TOUROKUBI, 'YYYY/MM/DD HH24:MI:SS')  = '" . $params['sid'] . "' ";

			//=====データ削除=====
			$this->dbExecQuery($sql, $db_params, $db,"T98SEARCH_PARAM");

			$intUCD = $params['UserCd'];
			$strAreaCd = str_replace(" ","",str_replace(",","",$params['AreaCd']));
			$strPrefCd = str_replace(" ","",$params['PrefCd']);
			$strSkchsn = str_replace(" ","",$params['CityCd']);
			$strMachi = str_replace(" ","",$params['SectCd']);
			$strEnsen = str_replace(" ","",$params['RailCd']);
			$strEki = str_replace(" ","",$params['StCd']);
			$intTinryoMin = trim($params['tinryo1']);
			$intTinryoMax = trim($params['tinryo2']);
			$intEkitoho = $params['ekitoho'];
			$intSenyuMin = $params['senyu1'];
			$intSenyuMax = $params['senyu2'];
			$intBuilt = $params['built'];
			$strSrhKbn = $params['SrhKbn'];
			//初期費用
			$intsyokihiyo = $params['syokihiyo'];

			//管理費・共益費
			if ($params['tinryo_jouken2'] == "on")
			{
				$IntKanriKyoueki = 1;
			}else{
				$IntKanriKyoueki = 0;
			}
			//敷金・礼金0
			if ($params['shikireizero'] == "on")
			{
				$IntShikiRei = 1;
			}else{
				$IntShikiRei = 0;
			}
			//フリーレント
			if ($params['freerent'] == "on")
			{
				$intFreeRent = 1;
			}else{
				$intFreeRent = 0;
			}
			//部屋割
			if ($params['heyawari'] == "on")
			{
				$intHeyaWari = 1;
			}else{
				$intHeyaWari = 0;
			}
			//建物種別
			for ($i = 1; $i <= 7; $i++)
			{
				if ($params['syubetu' . $i] == "on")
				{
					if ($strTatemonoType <> "")
					{
						$strTatemonoType .= ",";
					}
					$strTatemonoType .= $i;
				}
			}
			//間取
			for ($i = 10; $i <= 51; $i++)
			{
				if ($params['madori' . $i] == "on")
				{
					if ($strMadori <> "")
					{
						$strMadori .= "," ;
					}
					$strMadori .= $i;
				}
			}
			//受渡用パラメータタグ設定処理
			$rosenzu = explode(",",$params['rosenzucd_sel']);
			if (!empty($rosenzu)) {
				if (empty($rosenzu[0])) { $rosenzu = array(); }
			}
			for ($i = 0; $i < count($rosenzu); $i++)
			{
				if ($strEki <> "")
				{
					$strEki .= ",";
				}
				$strEki .= trim($rosenzu[$i]);
			}

			//情報の新しさ
			$intJyohou = $params['jyohou'];
			if ($intJyohou == "")
			{
				$intJyohou = 0;
			}
			//通勤時間
			$strEndRangeTime = str_replace(" ","",$params['EndRangeTime']);
			$strTgtStCd = str_replace(" ","",$params['TgtStCd']);
			if ($strEndRangeTime == "" || $strTgtStCd == "")
			{
				$strCommuting = "";
			}else{
				$strCommuting = $strTgtStCd . "," . $strEndRangeTime;
			}
			//キャンペーン対象検索
			if ($params['campaign'] == "on" || $params['campaign2'] == "on"
			|| $params['campaign3'] == "on" || $params['campaign4'] == "on"
			|| $params['campaign5'] == "on" || $params['campaign6'] == "on"
			|| $params['campaign7'] == "on" || $params['campaign8'] == "on"
			|| $params['campaign9'] == "on" || $params['campaign10'] == "on")
			{
				$intCampaign = 1;
			}else{
				$intCampaign = 0;
			}
			//こだわり条件
			$strKodawariJoken = $this->KodawariSet($params);

			//駐車場代込み
			if ($params['tyusyajyo_komi'] == "on")
			{
				$intTyusyajoKomi = 1;
			}else{
				$intTyusyajoKomi = 0;
			}
			//めやす賃料
			if ($params['meyasu_tinryo'] == "on")
			{
				$intMeyasuTinryo = 1;
			}else{
				$intMeyasuTinryo = 0;
			}
			//敷金なし
			if ($params['shikikin_nashi'] == "on")
			{
				$intShikikinNashi = 1;
			}else{
				$intShikikinNashi = 0;
			}
			//礼金なし
			if ($params['reikin_nashi'] == "on")
			{
				$intReikinNashi = 1;
			}else{
				$intReikinNashi = 0;
			}
			//バス利用可
			if ($params['bas_riyou'] == "on")
			{
				$intBasRiyou = 1;
			}else{
				$intBasRiyou = 0;
			}

			 $sql = "";
			 $sql .= " INSERT INTO T98SEARCH_PARAM ";
			 $sql .= "(";
			 $sql .= " T98_USER_CD ";
			 $sql .= " ,T98_AREA_CD ";
			 $sql .= " ,T98_TODOUFUKEN_CD ";
			 $sql .= " ,T98_SHIKUCHOUSON_CD ";
			 $sql .= " ,T98_CHOUMEI_CD ";
			 $sql .= " ,T98_ENSEN_CD ";
			 $sql .= " ,T98_EKI_CD ";
			 $sql .= " ,T98_CHINRYO_MIN ";
			 $sql .= " ,T98_CHINRYO_MAX ";
			 $sql .= " ,T98_CHINRYO_JKN1 ";
			 $sql .= " ,T98_CHINRYO_JKN2 ";
			 $sql .= " ,T98_TATEMONO_CD ";
			 $sql .= " ,T98_EKI_TOHO ";
			 $sql .= " ,T98_SENYUU_MENSEKI_MIN";
			 $sql .= " ,T98_SENYUU_MENSEKI_MAX ";
			 $sql .= " ,T98_MADORI ";
			 $sql .= " ,T98_CHIKUNENN_KBN ";
			 $sql .= " ,T98_KOUSHIN_KBN ";
			 $sql .= " ,T98_SEARCH_TYPE ";
			 $sql .= " ,T98_COMMUTING ";
			 $sql .= " ,T98_KODAWARI_JYOKEN ";
			 $sql .= " ,T98_TOUROKUBI ";
			 $sql .= " ,T98_MYPAGE_KBN ";
			 $sql .= " ,T98_CAMPAIGN_FLAG ";
			 $sql .= " ,T98_CHINRYO_JKN3 ";
			 $sql .= " ,T98_CHINRYO_JKN4 ";
			 $sql .= " ,T98_PARKING_KOMI_FLAG ";
			 $sql .= " ,T98_MEYASU_CHINRYO_FLAG ";
			 $sql .= " ,T98_SHIKIKIN_ZERO_FLAG ";
			 $sql .= " ,T98_REIKIN_ZERO_FLAG ";
			 $sql .= " ,T98_BUS_RIYOU_FLAG ";
			 $sql .= " ,T98_SHOKI_HIYO_KBN ";
			 $sql .= " )";
			 $sql .= " VALUES ";
			 $sql .= " ( ";
			 $sql .= $intUCD . " ";
			 $sql .= " ,'" . $strAreaCd . "'";
			 $sql .= " ,'" . $strPrefCd . "'";
			 $sql .= " ,'" . $strSkchsn . "'";
			 $sql .= " ,'" . $strMachi . "'";
			 $sql .= " ,'" . $strEnsen . "'";
			 $sql .= " ,'" . $strEki . "'";
			 $sql .= " ,'" . $intTinryoMin . "'";
			 $sql .= " ,'" . $intTinryoMax . "'";
			 $sql .= " ,'" . $IntKanriKyoueki . "'";
			 $sql .= " ,'" . $IntShikiRei . "'";
			 $sql .= " ,'" . $strTatemonoType . "'";
			 $sql .= " ,'" . $intEkitoho . "'";
			 $sql .= " ,'" . $intSenyuMin . "'";
			 $sql .= " ,'" . $intSenyuMax . "'";
			 $sql .= " ,'" . $strMadori . "'";
			 $sql .= " ,'" . $intBuilt . "'";
			 $sql .= " ,'" . $intJyohou . "'";
			 $sql .= " ,'" . $strSrhKbn . "'";
			 $sql .= " ,'" . $strCommuting . "'";
			 $sql .= " ,'" . $strKodawariJoken . "'";
			 $sql .= " ,SYSDATE";
			 $sql .= " ,1";
			 $sql .= " ,'" . $intCampaign . "'";
			 $sql .= " ,'" . $intFreeRent . "'";
			 $sql .= " ,'" . $intHeyaWari . "'";
			 $sql .= " ,'" . $intTyusyajoKomi . "'";
			 $sql .= " ,'" . $intMeyasuTinryo . "'";
			 $sql .= " ,'" . $intShikikinNashi . "'";
			 $sql .= " ,'" . $intReikinNashi . "'";
			 $sql .= " ,'" . $intBasRiyou . "'";
			 $sql .= " ,'" . $intsyokihiyo . "'";
			 $sql .= " )";

			 //=====データ登録=====
			 $this->dbExecQuery($sql, $db_params, $db,"T98SEARCH_PARAM");

			$sql = "";
			$sql .= "SELECT TO_CHAR(MAX(T98_TOUROKUBI), 'YYYY/MM/DD HH24:MI:SS') AS MAXDATE";
			$sql .= " FROM ";
			$sql .= " T98SEARCH_PARAM ";
			$sql .= " WHERE ";
			$sql .= " T98_USER_CD = " . $intUCD;

			//=====データ取得=====
			$userDtl = $this->dbGetList($sql, $db_params);
			$this->commitTransaction($db);
         }
		 catch (Exception $e)
		 {
		 	$this->rollbackTransaction($db);
		 	$resCd = "9907";
		 }
		 $db->closeConnection();

		$resCd = "0004";


		//結果コード変換
		$result = Apm_Otheritems::getResMessage($resCd);

		return array('result' => $result,'sid' => $userDtl[0]['MAXDATE']);
    }


    /**
	 * DBインサート用こだわり条件配列作成
	 *
	 * @param array $params
	 */
	private function KodawariSet($params)
	{
		$strField = "";
		//DBパラメータ用配列生成
    	 $db_params = array();

		$sql = "";
		$sql .= "SELECT W74_KODAWARI_ID ";
		$sql .= "FROM ";
		$sql .= "W74KODAWARI ";
		$sql .= "ORDER BY W74_KODAWARI_ID DESC";

		//=====データ取得=====
		$userDtl = $this->dbGetList($sql, $db_params);

		//ﾃﾞｰﾀが存在していた場合
		if ($userDtl[0]['W74_KODAWARI_ID'] <> "")
		{
			//現在のこだわりId最大値
			$strKIdMax = (int)$userDtl[0]['W74_KODAWARI_ID'];

		        for ($i = 1; $i <= $strKIdMax; $i++)
		        {
		        	if (($params['kodawari' . $i]) == "on")
		        	{
		        		$strField .= "1";
		        	}else{
		        		$strField .= "0";
		        	}
		        }
		}
	return $strField;
	}

}
?>
